<?php

namespace app\api\controller;

use app\BaseController;

use app\common\model\StaffModel;

use app\common\model\FirmStyleModel;

use app\common\model\VisitorModel;

use think\facade\Cache;

use app\common\OpenApi;

class Staff extends BaseController
{
    public function card(){
        $staff_info = $this->request->staff_info;
        $cache = $this->request->param('cache/d',0);
        if($cache === 1 || null === $res = Cache::get('staff_style_'.$staff_info->staff_id)){
            $res = FirmStyleModel::field('global,card,menu,page')->find($staff_info->firm_id);
            $res['staff_info'] = $staff_info->append(['detail','firm','profile']);
            Cache::set('staff_style_'.$staff_info->staff_id,$res,3600);
            $res['cache'] = 'new';
        }
        // 添加记录
        $this->addVisitor();
        return success($res);
    }
    public function visitor(){
        // 添加记录
        $res = $this->addVisitor();
        return success('记录添加成功');
    }
    // 获取员工介绍
    public function profile(){
        $res = $this->request->staff_info->append(['custom']);
        return success($res);
    }
    // 添加记录
    public function addVisitor(){
        $user_info = $this->request->user_info;
        $staff_info = $this->request->staff_info;
        if($user_info && $user_info->user_id > 0){
            $data = [
                'user_id' => $user_info->user_id,
                'staff_id' => $staff_info->staff_id
            ];
            if(null === $VisitorModel = VisitorModel::where($data)->cache(true)->find()){
                $VisitorModel = new VisitorModel();
                $VisitorModel->user_id = $user_info->user_id;
                $VisitorModel->firm_id = $staff_info->firm_id;
                $VisitorModel->staff_id = $staff_info->staff_id;
                $VisitorModel->save(); 
            }else{
                $VisitorModel->inc('view',1)->save();
            }
        }
        return true;
    }
    
    public function shareQrCode(){
        $code = $this->request->param('code/d',0);
        if($code === 0){
            return error('参数错误');
        }
        $firm_id = $this->request->param('firm_id/d',0);
        if($firm_id === 0){
            return error('参数错误');
        }
        $dir = root_path().'/public';
        
        $path = '/upload/qr/'.$this->request->staff_info->firm_id.'/share/';
        
        !is_dir($dir.$path) && mkdir($dir.$path, 0755, true);
        
        $path = $path.$firm_id.'_'.$code.'.png';
        if(!file_exists($dir.$path)){
            $data = ['code' => $code];
            $OpenApi = new OpenApi($firm_id);
            $res = $OpenApi->openApiPostImage('miniapp/getShareQrCode',$data);
            file_put_contents($dir.$path, $res);
        }
        return success(['qr_url'=>'https://'.$_SERVER['SERVER_NAME'].$path]);
    }
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        if($keyword === ''){
            $list = StaffModel::with(['detail','firm'])
            ->where('firm_id',$this->request->staff_info->firm_id)
            ->where('is_delete',0)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('staff_id','desc')
            ->select();
        }else{
            $list = StaffModel::alias('s')
                ->join('staff_detail d', 's.staff_id = d.staff_id')
                ->join('firm_detail f', 's.firm_id = f.firm_id')
                ->where('s.firm_id',$this->request->staff_info->firm_id)
                ->where('s.is_delete',0)
                ->where('s.name|s.mobile|s.position|d.name_en|f.company_name','like','%'.$keyword.'%')
                ->limit($this->request->limit)
                ->page($this->request->page)
                ->order('s.update_time','desc')
                ->select();
                // ->append(['staff','firm']);
        }
        
        return success($list);
    }
}
